package com.aisile.crowd_funding.system.service.impl;

import com.aisile.crowd_funding.common.utils.MD5Utils;
import com.aisile.crowd_funding.common.utils.PageUtils;
import com.aisile.crowd_funding.common.utils.Query;
import com.aisile.crowd_funding.common.utils.StringUtils;
import com.aisile.crowd_funding.system.mapper.SysUserMapper;
import com.aisile.crowd_funding.system.mapper.SysUserRoleMapper;
import com.aisile.crowd_funding.system.pojo.SysUser;
import com.aisile.crowd_funding.system.pojo.SysUserExample;
import com.aisile.crowd_funding.system.pojo.SysUserRole;
import com.aisile.crowd_funding.system.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @标题 UserServiceImpl
 * @作者 Mr.chen
 * @创建日期 2018/12/5 16:43
 * @版本号 1.0
 **/
@Service
@Transactional
public class UserServiceImpl implements UserService{
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return sysUserMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SysUser record) {
        String encrypt = MD5Utils.encrypt(record.getPassword());
        record.setPassword(encrypt);
        int insert = sysUserMapper.insert(record);
        //添加  用户和角色的关联数据
        Long userId = record.getUserId();
        Long[] roleIds = record.getRoleIds();
        if(roleIds!=null){
            for(Long roleId : roleIds){
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setRoleId(roleId);
                sysUserRole.setUserId(userId);
                sysUserRoleMapper.insert(sysUserRole);
            }
        }
        return insert;
    }

    @Override
    public SysUser selectByPrimaryKey(Long id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(SysUser sysUser) {
        return sysUserMapper.updateByPrimaryKey(sysUser);
    }

    @Override
    public List<SysUser> selectByExample(SysUserExample sysUserExample) {
        return sysUserMapper.selectByExample(sysUserExample);
    }

    @Override
    public SysUser findByUserName(String userName) {
        SysUserExample sysUserExample = new SysUserExample();
        sysUserExample.createCriteria().andUsernameEqualTo(userName);
        List<SysUser> sysUsers = sysUserMapper.selectByExample(sysUserExample);
        if(sysUsers!=null&&sysUsers.size()>0){
            return sysUsers.get(0);
        }
        return null;
    }

    @Override
    public PageUtils selectByPage(Map<String,String> map) {
        Query.startPage(map);
        String name = map.get("name");
        String username = map.get("username");
        SysUserExample example = new SysUserExample();
        if(!StringUtils.isBlank(name)){
            example.createCriteria().andNameLike("%"+name+"%");
        }
        if(!StringUtils.isBlank(username)){
            example.createCriteria().andUsernameLike("%"+username+"%");
        }
        Page<SysUser> page = (Page<SysUser>)selectByExample(example);
        PageUtils pageUtils = new PageUtils(page.getResult(),page.getTotal());
        return pageUtils;
    }


}
